var mongoose = require('mongoose');
var newsComment = require("../model/newsComment");
var News = require('../model/news.model');
module.exports={
    getNews(req,res,next){
        var id = req.query.id;
        News.findById(id,(error,results)=>{
            if(error){
                console.log(error)
            }
            else{
                res.json(results);
            }
        })
    },
    list(req,res,next){
        var page=(req.body.page)?req.body.page:1;
        var limit=(req.body.limit)?req.body.limit:2;
        var queyCondition={
            
        };
        if(req.params.id && req.params.id.trim().length>0){
            queryCondition ={
                "userId": req.params.id
            }
        }
        if(req.body.name&&req.body.name.trim().length>0){
            name=req.body.name;
            queyCondition =object.assign(queyCondition,{
                name: new RegExp(name,"i")
            })
            
        }
        newsComment.paginate(queyCondition,{page:page,limit:limit},(error,results)=>{
            if(error){
                console.log(error)
            }
            else{
                res.json(results);
            }
        })
    },
    post(req,res,next){

        console.log(req.body);
        const comment=new newsComment(req.body);
        comment.save(function(error,result){
            if(error){
                res.json(error);
            }
            else{
                res.json(result);
            }
        })
    },
    put(req,res,next){
        const id=req.params.id;
        newsComment .findByIdAndUpdate(id,{$set:req.body},{new:false}).then((result)=>{
                    res.json(result);
            })
    },
    delete(req,res,next){
        const id=req.params.id;
        newsComment.findByIdAndRemove(id,function(error,result){
            if(error){
                console.log(error)
            }
            else{
                res.json("delete is ok!")
            } 
           
        })
    },
    deletemany(req,res,next){
        const id=req.body.id;
        newsComment.remove({"_id":{ $in:id}},function(error,result){
            if(error){
                console.log(error)
            }
            else{
                res.json("deletemany is ok!")
            } 
            
        })
    }
}